home *** CD-ROM | disk | FTP | other *** search
- Path: in2.uu.net!demos!usenet
- From: Alexey Ruzin <00alex@dbs.demos.su>
- Newsgroups: comp.lang.c++
- Subject: Re: How to Get all Permutations
- Date: Wed, 17 Apr 1996 17:52:23 +0400
- Organization: Demos Online Service
- Message-ID: <3174F797.2D8F@dbs.demos.su>
- References: <3174417f.15405235@sun.cis.smu.edu>
- NNTP-Posting-Host: 00alex@dbs.demos.su
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (X11; I; SunOS 5.4 sun4m)
-
- Damon Bowman wrote:
- >
- > I'm writing a program in which I need to fill an array with all
- > possible combinations of a group of numbers.
- >
- > For example, say you have the following table:
- > Choices - A B C
- > -------------------
- > Person 1 - 1 2 3
- > Person 2 - 2 3 1
- > Person 3 - 3 1 2
- >
- > This is one possible combination out of 216 (#choices!)^#persons =
- > 3!^3 = 216.
- >
- > In other words, each person must rank three choices in order of
- > preference. I am trying to fill an array with every possible
- > combination of all three persons' choices.
- >
-
- If i got all right, you may do so:
- Write a class named 'Choice' which will represent one of all
- possible variants.
-
- #define NUMCHOICES 3
- #define NUMPERSONS 3
-
- class Choice
- {
- public:
- int choice[NUMCHOICES];
- ...
- };
-
- Now fill a massive of all possible choices:
-
- Choice all[NUMCHOICES!]; /* ! factorial is not C++ method too */
-
- Then each person have its own choice, so NUMPERSONS persons are
- one variant of set, and there are NUMCHOICES!^3 variants in the set:
-
- int election[NUMPERSONS][NUMCHOICES!^3]; /* power is not C++ method */
-
- This is better (snd simple) to use recursions to fill out these arrays.
-
- I hope you know how to write recurse functions. If not
- write to conference reply on my message, I explain.
-
- Good luck.
- Alex.
-